package io.renren.modules.sys.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.service.SysUserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户注册
 * 
 * @author wxj
 * @date 2017年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/app/pc/register")
public class SysRegisterController extends AbstractController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserTokenService sysUserTokenService;

	/**
	 * 注册用户
	 */
//	@SysLog("注册用户")
	@RequestMapping("/do")
	public R save(@RequestBody SysUserEntity user){
		ValidatorUtils.validateEntity(user, AddGroup.class);

		List<Long> roleList = new ArrayList();
		roleList.add((long) Constant.NORMAL_USER);

		user.setRoleIdList(roleList);

		sysUserService.saveWithOutCheck(user);

		//生成token，并保存到数据库
		R r = sysUserTokenService.createToken(user.getUserId());
		return r;
		
	}
	
}
